home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue64 / Clinic / PrinterSettingsU.pas < prev   
Encoding:
Pascal/Delphi Source File  |  2000-10-31  |  2.4 KB  |  102 lines

  1. unit PrinterSettingsU;
  2.  
  3. {$ifdef Ver90} { Delphi 2.0x }
  4.   {$define DelphiLessThan3}
  5. {$endif}
  6. {$ifdef Ver93} { C++ Builder 1.0x }
  7.   {$define DelphiLessThan3}
  8. {$endif}
  9.  
  10. interface
  11.  
  12. uses
  13.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  14.   StdCtrls, ExtCtrls;
  15.  
  16. type
  17.   TForm1 = class(TForm)
  18.     btnLocalSettings: TButton;
  19.     dlgSettings: TPrinterSetupDialog;
  20.     rgLocalOrientation: TRadioGroup;
  21.     btnGlobalSettings: TButton;
  22.     procedure btnLocalSettingsClick(Sender: TObject);
  23.     procedure rgLocalOrientationClick(Sender: TObject);
  24.     procedure btnGlobalSettingsClick(Sender: TObject);
  25.   private
  26.     { Private declarations }
  27.   public
  28.     { Public declarations }
  29.   end;
  30.  
  31. var
  32.   Form1: TForm1;
  33.  
  34. implementation
  35.  
  36. {$R *.DFM}
  37.  
  38. uses
  39.   Printers, WinSpool;
  40.  
  41. {$ifdef DelphiLessThan3}
  42. type
  43.   EWin32Error = class(Exception);
  44.  
  45. function Win32Check(RetVal: BOOL): BOOL;
  46. begin
  47.   if not RetVal then
  48.     raise EWin32Error.Create(SysErrorMessage(GetLastError));
  49.   Result := RetVal;
  50. end;
  51. {$endif}
  52.  
  53. procedure TForm1.btnLocalSettingsClick(Sender: TObject);
  54. begin
  55.   dlgSettings.Execute
  56. end;
  57.  
  58. procedure TForm1.rgLocalOrientationClick(Sender: TObject);
  59. var
  60.   Device, Driver, Port: array[0..255] of Char;
  61.   DevModeHdl: THandle;
  62.   DevModePtr: PDevMode;
  63. begin
  64.   Printer.GetPrinter(Device, Driver, Port, DevModeHdl);
  65.   if DevModeHdl <> 0 then
  66.   begin
  67.     DevModePtr := GlobalLock(DevModeHdl);
  68.     if Assigned(DevModePtr) then
  69.       try
  70.         if DevModePtr^.dmFields and dm_Orientation = 0 then
  71.           raise EPrinter.Create('Custom paper orientations not supported');
  72.         case (Sender as TRadioGroup).ItemIndex of
  73.           0: DevModePtr^.dmOrientation := DMORIENT_PORTRAIT;
  74.           1: DevModePtr^.dmOrientation := DMORIENT_LANDSCAPE;
  75.         end
  76.       finally
  77.         GlobalUnlock(DevModeHdl)
  78.       end;
  79.     Printer.SetPrinter(Device, Driver, Port, DevModeHdl);
  80.   end
  81. end;
  82.  
  83. procedure TForm1.btnGlobalSettingsClick(Sender: TObject);
  84. var
  85.   Device, Driver, Port: array[0..255] of Char;
  86.   DevModeHdl, PrinterHandle: THandle;
  87. begin
  88.   //Get printer device name
  89.   Printer.GetPrinter(Device, Driver, Port, DevModeHdl);
  90.   //Get printer handle
  91.   Win32Check(OpenPrinter(Device, PrinterHandle, nil));
  92.   try
  93.     //Invoke the printer property sheet
  94.     Win32Check(PrinterProperties(Handle, PrinterHandle));
  95.   finally
  96.     //Close the opened handle
  97.     ClosePrinter(PrinterHandle)
  98.   end;
  99. end;
  100.  
  101. end.
  102.